package tms.controler;

import java.util.List;

import javax.swing.JPanel;

import tms.model.Booking;
import tms.model.Flight;
import tms.view.SummaryPanel;
import tms.view.TravelOptionView;

/**
 *  This class handles flight searched results.
 */
public class TravelOptionController implements IController {

    private TravelOptionView view;
    private SummaryController bsc;

    /*
     * We need BookingSummaryController to get information for searching
     */
    public TravelOptionController(SummaryController bsc) {
        this.bsc = bsc;
    }

    /**
     * Update the departTicket & returnTicket with the selected flight that
     * users choose from tables.
     */
    public void updateTicket() {
        Flight selectedDepart = view.getSelectedDepartFlight();
        Flight selectedReturn = view.getSelectedReturnFlight();

        // this is for the case of the view has just loaded
        if (selectedDepart == null) {
            return;
        }

        Booking departTicket = bsc.getDepatureTicket();
        if (departTicket != null) {
            departTicket.setFlight(selectedDepart);
            // and update the ticket
            bsc.setDepatureTicket(departTicket);
        }

        Booking returnTicket = null;
        if (selectedReturn != null) {
            returnTicket = bsc.getReturnTicket();
            if (returnTicket != null) {
                returnTicket.setFlight(view.getSelectedReturnFlight());
                bsc.setReturnTicket(returnTicket);
            }
        }
        bsc.bindSummaryInfo(departTicket, returnTicket);
    }

    public void bindSearchResults(List<Flight> departResult, List<Flight> returnResult) {
        assert (view != null);

        // bind depart table
        assert (departResult != null);
        view.setDepartureTableData(convertToFlightTableData(departResult));

        // bind return table
        assert (returnResult != null);
        view.setReturnTableData(convertToFlightTableData(returnResult));
    }

    public void moveToBookingContactInfo() {
        ContactInfoController bcic = new ContactInfoController(bsc);
        bcic.createView();

        // change to the next view - which is TravelOptionPanel
        BaseController.getInstance().changeToPanel((JPanel) bcic.getView());
    }

    public void createView() {
        view = new TravelOptionView(this);
        view.addBookingSummaryPanel((SummaryPanel) bsc.getView());
    }

    /**
     * Convert a list of flights to the corresponding 2-dimension array which
     * will be use as data in JTable Model.
     * See FlightTableModel at TravelOptionPanel
     * @param flights
     * @return
     */
    public Object[][] convertToFlightTableData(List<Flight> flights) {
        assert (flights != null);
        Object[][] flightsData = new Object[flights.size()][2];
        int i = 0;
        for (Flight f : flights) {
            // the first column is a Flight object
            flightsData[i][0] = f;
            // the second is a CheckBox
            flightsData[i][1] = Boolean.FALSE;
            ++i;
        }
        return flightsData;
    }

    public Object getView() {
        if (view == null) {
            createView();
        }
        return view;
    }


}
